package com.yzw.helloword;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

import java.nio.charset.Charset;

/**
 * @Author: YangZhenWei
 * @PROJECT_NAME: nettyTeach
 * @DESCRIPTION: 通用handler，处理IO事件
 * @USER: root
 * @DATE: 2023/2/8 22:35
 **/
@ChannelHandler.Sharable
public class HandlerClientHello extends SimpleChannelInboundHandler<ByteBuf> {
    /**
     * 通道读写文件,处理读取信息
     *
     * @param channelHandlerContext 通道
     * @param byteBuf 缓存区
     * @throws Exception
     */
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {
        System.out.println("接收到的消息"+byteBuf.toString(CharsetUtil.UTF_8));

    }

    /**
     *  处理 io事件的异常
     *
     * @param ctx
     * @param cause
     * @throws Exception 抛出异常
     *
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close(); }
}
